﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Mvc.Html;
using BPM.Service;
using BPM.Web;
using System.Linq.Expressions;
using BPM.Entity;
using hxy.CopyRight;

namespace BPM
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            #region AuthorityController

            //列表显示未验证用户
            MapRoute(routes, a => a.Authority.WaitForValidUserList, "page");

            //列表显示所有已验证用户
            MapRoute(routes, a => a.Authority.UserList, "page");

            //允许一个未验证用户的注册申请
            MapRoute(routes, a => a.Authority.AssignAuthority, "userId");

            //允许一个未验证用户的注册申请
            MapRoute(routes, a => a.Authority.AllowNotValidUser, "userId");

            #endregion

            #region MaterialManagement

            //MapRoute(routes, a => a.MaterialManagement.ViewMaterialList, "page");
            MapRoute(routes, a => a.MaterialManagement.ModifyMaterial, "id");
            MapRoute(routes, a => a.MaterialManagement.Delete, "id");
            MapRoute(routes, a => a.MaterialManagement.ViewPendingMaterialList, "pageIndex");
            MapRoute(routes, a => a.MaterialManagement.ViewPendingMaterial, "id");
            MapRoute(routes, a => a.MaterialManagement.ApprovePendingMaterial, "id");
            MapRoute(routes, a => a.MaterialManagement.DeletePendingMaterial, "id");

            #endregion

            #region PurchaseOrder

            //库管员查看他提交的进货单列表
            MapRoute(routes, p => p.PurchaseOrder.MyPurchaseOrderList, "page");
            //库管查看一个他提交的进货单
            MapRoute(routes, p => p.PurchaseOrder.MyPurchaseOrder, "id");
            //查看未审核的采购单列表
            MapRoute(routes, p => p.PurchaseOrder.PendingPurchaseOrderList, "pageIndex");
            //查看某个未审核的采购单详细
            MapRoute(routes, p => p.PurchaseOrder.ViewPendingPurchaseOrder, "id");
            //通过一个待审核的采购单
            MapRoute(routes, p => p.PurchaseOrder.ApprovePendingPurchaseOrder, "id");
            //删除一个待审核的采购单
            MapRoute(routes, p => p.PurchaseOrder.DeletePendingPurchaseOrder, "id");

            #endregion

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/", // URL with parameters
                new { controller = AD.Singleton.Logon.ToString(), action = AD.Singleton.Logon.Logon } // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            DependencyInjection();

            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            new EntitiesInitializer().InitSysEntites();
        }

        private static void DependencyInjection()
        {
			ProductRegister.RegisterInfoProvider = new FileRegisterInfoProvider(@"D:\BPM_Authotication_Code.txt");
            LazyProviderFactoryHost.Factory = ServiceFactory.Instance;
            DPHost.Context = new ControllerUserStrategy_HttpSession();
        }

        public static Route MapRoute(RouteCollection routes,
            Expression<Func<AD, string>> actionExp,
            string paramName = null,
            object defaultValue = null
            )
        {
            var ad = AD.Read(actionExp);
            var name = ad.Format();
            var url = ad.Format() + "{" + paramName + "}";

            Route route = new Route(url, new MvcRouteHandler());
            route.Defaults = new RouteValueDictionary();
            route.Defaults.Add("controller", ad.Controller);
            route.Defaults.Add("action", ad.Action);
            if (paramName != null)
            {
                route.Defaults.Add(paramName, defaultValue ?? UrlParameter.Optional);
            }
            route.Constraints = new RouteValueDictionary();
            route.DataTokens = new RouteValueDictionary();
            routes.Add(name, route);
            return route;
        }
    }
}